// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Act Finish Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias object.xpos   : object.decorPos
private alias object.ypos   : object.actPos
private alias object.value0 : object.timer
private alias object.value1 : object.playerNamePos
private alias object.value2 : object.passedPos
private alias object.value3 : object.totalPos
private alias object.value4 : object.timePos
private alias object.value5 : object.timeBonus
private alias object.value6 : object.ringBonus
private alias object.scale  : object.ringPos

private alias object.value0 : player.rings

// States
private alias 0  : ACTFINISH_INIT
private alias 1  : ACTFINISH_ENTERACTFINISH
private alias 2  : ACTFINISH_ENTERPASSEDTEXT
private alias 3  : ACTFINISH_ENTERACTID
private alias 4  : ACTFINISH_ENTERBONUSES
private alias 5  : ACTFINISH_ACTFINISHJINGLE
private alias 6  : ACTFINISH_TALLYSCORE
private alias 7  : ACTFINISH_SHOWRESULTS
private alias 8  : ACTFINISH_LOADNEXTSTAGE
private alias 9  : ACTFINISH_EXITUISPRITES
private alias 10 : ACTFINISH_EXITTEXT
private alias 11 : ACTFINISH_EXITSCORES

// Static Values

// Tables

event ObjectMain
	switch object.state
	case ACTFINISH_INIT
		object.drawOrder = 6
		object.priority = PRIORITY_ACTIVE

		object.decorPos = screen.xcenter
		object.decorPos += 128
		object.actPos = screen.xcenter
		object.playerNamePos = screen.xcenter
		FlipSign(object.playerNamePos)
		object.playerNamePos -= 160
		object.passedPos = screen.xcenter
		FlipSign(object.passedPos)
		object.passedPos -= 160
		object.totalPos = screen.xcenter
		object.totalPos += 128
		object.timePos = object.totalPos
		object.timePos += 32
		object.ringPos = object.timePos
		object.ringPos += 32

		if PlayerObject_SuperState == 1
			PlayerObject_SuperState = 2
		end if

		switch stage.minutes
		case 0
			if stage.seconds < 30
				object.timeBonus = 50000
			else
				if stage.seconds < 45
					object.timeBonus = 10000
				else
					object.timeBonus = 5000
				end if
			end if
			break

		case 1
			if stage.seconds < 30
				object.timeBonus = 4000
			else
				object.timeBonus = 3000
			end if
			break

		case 2
			object.timeBonus = 2000
			break

		case 3
			object.timeBonus = 1000
			break

		case 4
			object.timeBonus = 500
			break

		case 5
			object.timeBonus = 100
			break

		case 9
			if stage.debugMode == 0
				if options.gameMode < 2
					if stage.seconds == 59
						object.timeBonus = 100000
					end if
				end if
			end if
			break

		end switch

		object.ringBonus = player[0].rings
		object.ringBonus *= 100
		object.state++
		break

	case ACTFINISH_ENTERACTFINISH
		if object.decorPos > 0
			object.decorPos -= 16
			if object.decorPos < 0
				object.decorPos = 0
			end if
		end if

		if object.playerNamePos < 0
			object.playerNamePos += 16
			if object.playerNamePos > 0
				object.playerNamePos = 0
			end if
		else
			object.state++
		end if
		break

	case ACTFINISH_ENTERPASSEDTEXT
		if object.passedPos < 0
			object.passedPos += 16
			if object.passedPos > 0
				object.passedPos = 0
			end if
		else
			object.state++
		end if
		break
		
	case ACTFINISH_ENTERACTID
		if object.actPos > 0
			object.actPos -= 16
			if object.actPos < 0
				object.actPos = 0
			end if
		else
			object.state++
		end if
		break

	case ACTFINISH_ENTERBONUSES
		if object.totalPos > 0
			object.totalPos -= 16
			if object.totalPos < 0
				object.totalPos = 0
			end if
		end if

		if object.timePos > 0
			object.timePos -= 16
			if object.timePos < 0
				object.timePos = 0
			end if
		end if

		if object.ringPos > 0
			object.ringPos -= 16
			if object.ringPos < 0
				object.ringPos = 0
			end if
		else
			object.state++
		end if
		break

	case ACTFINISH_ACTFINISHJINGLE
		object.timer++
		if object.timer > 299
			object.timer = 0
			object.state++
		end if
		break

	case ACTFINISH_TALLYSCORE
		if object.ringBonus > 0
			object.ringBonus -= 100
			player.score += 100
		end if

		if object.timeBonus > 0
			object.timeBonus -= 100
			player.score += 100
		end if

		CheckGreater(object.ringBonus, 0)
		temp0 = checkResult
		CheckGreater(object.timeBonus, 0)
		temp0 |= checkResult
		if temp0 == true
			object.timer++
			if object.timer == 2
				PlaySfx(SfxName[Score Add], false)
				object.timer = 0
			end if
		else
			object.state++
			object.timer = 0
			PlaySfx(SfxName[Score Total], false)
		end if
		break

	case ACTFINISH_SHOWRESULTS
		object.timer++
		if object.timer == 160
			object.timer = 0
			if options.gameMode != 2
				CheckCurrentStageFolder("Zone06")
				temp0 = checkResult
				CheckEqual(stage.actNum, 2)
				temp0 &= checkResult
				if temp0 == true
					object.state = ACTFINISH_EXITUISPRITES
				else
					object.state++
					if object[0].type == TypeName[Blank Object]
						PlaySfx(SfxName[Warp], false)
					end if
				end if
			else
				object.state++
				if object[0].type == TypeName[Blank Object]
					PlaySfx(SfxName[Warp], false)
				end if
			end if
		end if
		break

	case ACTFINISH_LOADNEXTSTAGE
		if object[0].type == TypeName[Blank Object]
			object.timer += 8
			SetScreenFade(208, 255, 224, object.timer)
			if object.timer == 0x400
				fadeColor = 208
				fadeColor <<= 16
				temp0 = 255
				temp0 <<= 8
				fadeColor += temp0
				fadeColor += 224
				object.direction = FLIP_NONE
				object.timer = 0
				lampPostID = 0
				stage.listPos++
				if options.gameMode == 1
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					if stage.player2Enabled == true
						saveRAM[arrayPos1] = 3
					else
						saveRAM[arrayPos1] = stage.playerListPos
					end if
					arrayPos1++
					saveRAM[arrayPos1] = player.lives
					arrayPos1++
					saveRAM[arrayPos1] = player.score
					arrayPos1++
					saveRAM[arrayPos1] = player.scoreBonus
					arrayPos1++
					temp0 = stage.listPos
					temp0++
					if temp0 > saveRAM[arrayPos1]
						saveRAM[arrayPos1] = temp0
						saveRAM[arrayPos1] += 0x80
					end if
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.emeralds
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.listPos
					WriteSaveRAM()
				end if

				specialStage.nextZone = stage.listPos
				stage.listPos = specialStage.listPos
				stage.activeList = SPECIAL_STAGE
				temp0 = engine.trialMode
				if stage.listPos >= stage.listSize
					temp0 = true
				end if
				if temp0 == false
					LoadStage()
				else
					// Invalid Stage, Go back to the title screen
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = 0
					LoadStage()
				end if
			end if
		else
			object.timer += 4
			SetScreenFade(0, 0, 0, object.timer)
			if object.timer == 384
				fadeColor = 0
				object.timer = 0
				lampPostID = 0
				object.direction = FLIP_NONE
				stage.listPos++
				if options.gameMode == 1
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					if stage.player2Enabled == true
						saveRAM[arrayPos1] = 3
					else
						saveRAM[arrayPos1] = stage.playerListPos
					end if
					arrayPos1++
					saveRAM[arrayPos1] = player.lives
					arrayPos1++
					saveRAM[arrayPos1] = player.score
					arrayPos1++
					saveRAM[arrayPos1] = player.scoreBonus
					arrayPos1++
					temp0 = stage.listPos
					temp0++
					if temp0 > saveRAM[arrayPos1]
						saveRAM[arrayPos1] = temp0
					end if
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.emeralds
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.listPos
					WriteSaveRAM()
				end if

				if options.gameMode == 2
					timeAttack.result = stage.seconds
					timeAttack.result *= 100
					temp0 = stage.minutes
					temp0 *= 0x1770
					timeAttack.result += temp0
					timeAttack.result += stage.milliSeconds
					stage.listPos--
					CallNativeFunction2(SetLeaderboard, stage.listPos, timeAttack.result)
					engine.state = 8
				else
					temp0 = engine.trialMode
					if stage.listPos >= stage.listSize
						temp0 = true
					end if

					if temp0 == false
						LoadStage()
					else
						// Invalid Stage, Go back to the title screen
						stage.activeList = PRESENTATION_STAGE
						stage.listPos = 0
						LoadStage()
					end if
				end if
			end if
		end if
		break

	case ACTFINISH_EXITUISPRITES
		object.decorPos += 32
		object.actPos += 32
		object.playerNamePos -= 32
		object.timer++
		if object.timer == 8
			object.timer = 0
			object.state++
		end if
		break

	case ACTFINISH_EXITTEXT
		object.decorPos += 32
		object.actPos += 32
		object.playerNamePos -= 32
		object.passedPos -= 32
		object.timer++
		if object.timer == 8
			object.timer = 0
			object.state++
		end if
		break

	case ACTFINISH_EXITSCORES
		object.decorPos += 32
		object.actPos += 32
		object.playerNamePos -= 32
		object.passedPos -= 32
		object.totalPos += 32
		if object.totalPos > 96
			object.timePos += 32
			if object.timePos > 96
				object.ringPos += 32
			end if
		end if

		object.timer++
		if object.timer == 35
			object.timer = 0
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			foreach (TypeName[Sign Post], arrayPos0, ACTIVE_ENTITIES)
				object[arrayPos0].state = 5
			next

			currentPlayer = 0
			while currentPlayer < playerCount
				object[currentPlayer].interaction 	= true
				object[currentPlayer].controlMode 	= 0
				object[currentPlayer].right 		= true
				currentPlayer++
			loop

			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
			SetMusicTrack("Final.ogg", 0, 84680)
			PlayMusic(0)
		end if
		break
		
	end switch
end event


event ObjectDraw
	SetActivePalette(0, 0, screen.ysize)

	temp0 = screen.xcenter
	temp0 += 16
	temp0 += object.decorPos
	DrawSpriteScreenXY(3, temp0, 56)

	temp0 = screen.xcenter
	temp0 += 3
	temp0 += object.playerNamePos
	DrawSpriteScreenXY(stage.playerListPos, temp0, 60)
	DrawSpriteScreenXY(4, temp0, 60)

	temp0 = screen.xcenter
	temp0 -= 48
	temp0 += object.passedPos
	DrawSpriteScreenXY(5, temp0, 80)

	temp0 = screen.xcenter
	temp0 += 24
	temp0 += object.actPos
	temp1 = stage.actNum
	temp1 += 5
	if temp1 < 6
		temp1 = 6
	end if
	if temp1 > 8
		temp1 = 8
	end if
	DrawSpriteScreenXY(temp1, temp0, 82)

	temp0 = screen.xcenter
	temp0 -= 79
	temp0 += object.totalPos
	DrawSpriteScreenXY(9, temp0, 117)

	temp0 += 151
	DrawNumbers(13, temp0, 170, player.score, 6, 8, 0)

	temp0 = screen.xcenter
	temp0 -= 79
	temp0 += object.timePos
	DrawSpriteScreenXY(10, temp0, 126)
	DrawSpriteScreenXY(12, temp0, 133)

	temp0 += 151
	DrawNumbers(13, temp0, 123, object.timeBonus, 6, 8, 0)

	temp0 = screen.xcenter
	temp0 -= 79
	temp0 += object.ringPos
	DrawSpriteScreenXY(11, temp0, 142)
	DrawSpriteScreenXY(12, temp0, 149)

	temp0 += 151
	DrawNumbers(13, temp0, 142, object.ringBonus, 5, 8, 0)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-80, 0, 70, 16, 1, 222) 		// ? - #0
	if options.region == false
		SpriteFrame(-80, 0, 69, 16, 72, 222) 	// ? - #1
	else
		SpriteFrame(-80, 0, 69, 16, 145, 205) 	// ? - #1
	end if
	SpriteFrame(-113, 0, 116, 16, 182, 188) 		// ? - #2

	if stage.playerListPos != PlayerName[KNUCKLES]
		SpriteFrame(0, 0, 56, 56, 41, 1) 		// ? - #3
		SpriteFrame(7, 0, 47, 16, 1, 205) 		// ? - #4
		SpriteFrame(-20, 0, 109, 16, 215, 205) 		// ? - #5
		SpriteFrame(30, -17, 49, 32, 257, 1) 		// ? - #6
		SpriteFrame(30, -17, 49, 35, 257, 34) 		// ? - #7
		SpriteFrame(30, -17, 49, 33, 257, 70) 		// ? - #8
	else
		SpriteFrame(0, 0, 56, 56, 41, 1) 		// ? - #3
		SpriteFrame(7, 0, 47, 16, 1, 205) 		// ? - #4
		SpriteFrame(-20, 0, 109, 16, 215, 205) 		// ? - #5
		SpriteFrame(30, -17, 49, 32, 257, 1) 		// ? - #6
		SpriteFrame(30, -17, 49, 35, 257, 34) 		// ? - #7
		SpriteFrame(30, -17, 49, 33, 257, 70)		// ? - #8
	end if
	SpriteFrame(15, 45, 48, 19, 266, 107)  		// ? - #9
	SpriteFrame(0, 0, 31, 11, 1, 17)  			// ? - #10
	SpriteFrame(0, 0, 31, 11, 1, 33)  			// ? - #11
	SpriteFrame(40, -16, 48, 20, 266, 127)  		// ? - #12
	SpriteFrame(0, 0, 8, 11, 1, 50)  			// ? - #13
	SpriteFrame(0, 0, 8, 11, 10, 50)  			// ? - #14
	SpriteFrame(0, 0, 8, 11, 19, 50)  			// ? - #15
	SpriteFrame(0, 0, 8, 11, 28, 50)  			// ? - #16
	SpriteFrame(0, 0, 8, 11, 1, 62)  			// ? - #17
	SpriteFrame(0, 0, 8, 11, 10, 62)  			// ? - #18
	SpriteFrame(0, 0, 8, 11, 19, 62)  			// ? - #19
	SpriteFrame(0, 0, 8, 11, 28, 62)  			// ? - #20
	SpriteFrame(0, 0, 8, 11, 1, 74)  			// ? - #21
	SpriteFrame(0, 0, 8, 11, 10, 74)  			// ? - #22
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
